﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Order.Api.Read.Application.Command;
using Order.Api.Read.Dto.Order;
using Order.ErrorCode;
using System;
using System.Threading.Tasks;

namespace Order.Api.Read.Controllers
{
    /// <summary>
    /// 药品订单控制器
    /// 提供药品订单相关的API接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DrugOrdersController : ControllerBase
    {
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        public DrugOrdersController(IMediator mediator)
        {
            _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator), "中介者不能为空");
        }

        /// <summary>
        /// 获取药品订单分页数据
        /// 支持关键字搜索、时间范围筛选、状态筛选
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>分页结果</returns>
        [HttpGet]
        public async Task<IActionResult> GetDrugOrdersPage([FromQuery] DrugOrdersQueryDto query)
        {
            try
            {
                // 构建查询命令
                var command = new DrugOrdersPageCommand
                {
                    Query = query ?? new DrugOrdersQueryDto()
                };

                // 发送命令并获取结果
                var result = await _mediator.Send(command);

                return Ok(new ApiResult<ApiPageIng<DrugOrdersPageDto>>
                {
                    Code = ApiEnums.Success,
                    Data = result,
                    Msg = "获取药品订单分页数据成功"
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new ApiResult<ApiPageIng<DrugOrdersPageDto>>
                {
                    Code = ApiEnums.Fail,
                    Msg = ex.Message
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new ApiResult<ApiPageIng<DrugOrdersPageDto>>
                {
                    Code = ApiEnums.Error,
                    Msg = $"获取药品订单分页数据时发生错误: {ex.Message}"
                });
            }
        }

        /// <summary>
        /// 获取药品订单详情
        /// 包含订单基本信息、收货人信息、货物信息列表
        /// </summary>
        /// <param name="id">药品订单ID</param>
        /// <returns>订单详情</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetDrugOrdersDetail(int id)
        {
            try
            {
                // 参数验证
                if (id <= 0)
                {
                    return BadRequest(new ApiResult<DrugOrdersDetailDto>
                    {
                        Code = ApiEnums.Fail,
                        Msg = "订单ID必须大于0"
                    });
                }

                // 构建查询命令
                var command = new DrugOrdersDetailCommand
                {
                    Id = id
                };

                // 发送命令并获取结果
                var result = await _mediator.Send(command);

                return Ok(new ApiResult<DrugOrdersDetailDto>
                {
                    Code = ApiEnums.Success,
                    Data = result,
                    Msg = "获取药品订单详情成功"
                });
            }
            catch (InvalidOperationException ex)
            {
                return NotFound(new ApiResult<DrugOrdersDetailDto>
                {
                    Code = ApiEnums.NotFound,
                    Msg = ex.Message
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new ApiResult<DrugOrdersDetailDto>
                {
                    Code = ApiEnums.Fail,
                    Msg = ex.Message
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new ApiResult<DrugOrdersDetailDto>
                {
                    Code = ApiEnums.Error,
                    Msg = $"获取药品订单详情时发生错误: {ex.Message}"
                });
            }
        }
    }
}
